package me.aristotll.converter

import groovy.transform.CompileStatic
import org.apache.log4j.Logger
import org.codehaus.groovy.antlr.java.Java2GroovyProcessor
import org.codehaus.groovy.runtime.ResourceGroovyMethods

/**
 * Name: Java2Groovy<br>
 * User: Yao<br>
 * Date: 17/6/24<br>
 * Time: 00:45<br>
 */
@CompileStatic
class Java2Groovy {
    private static final Logger logger = Logger.getLogger(Java2Groovy.class);

    static String convert(String filename) {
        StringBuilder s = new StringBuilder()
        File f = new File(filename)
        String text = ResourceGroovyMethods.getText(f)
        s.append(Java2GroovyProcessor.convert(filename, text, false, true))
        s.toString()
    }

    /**
     * todo enum 之类的乱换行
     * """"" 空字符串问题
     * "$1" 在字符串中的问题 或者字符串全部改成 '' 的
     * @param origin
     * @param converted
     */
    static void newConvertedFile(String origin, String converted) {
        def file = new File(converted)
        if (file.exists()) {
            logger.info("already existed");
            return
        }
        file << convert(origin)

    }


    static void main(String[] args) {

        final String converted = Java2Groovy.convert("src/me/aristotll/converter/TrueJavaToGroovyAction.java")
        println "" != converted
    }
}
